package part3;

/**
 * Title: Third class for CSIS 235 Project 8 part 3
 * Description: CS 235 Project #8 - Part 3
 *              Serialization
 * @author Anthony Varghese
 * @email  your.n.here@my.uwrf.edu
 * @date   February 5th 2012
 */

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Scanner;



public class Third {
	/**
	 * Data members
	 *  The static String named "filename" is used to hold the name of
	 *   the file that we will read from.
	 *       ** Note that when using Eclipse this file will be in the
	 *       ** package folder and not in the Project or src folders.
	 *       
	 *  The static PrintStream object named "out" is used for convenience - 
	 *   it is not really necessary. 
	 */
	private static final  String folder           = "src/part3/";
	private static final  String infilename       = folder + "Accounts.txt";
	private static final  java.io.PrintStream out = System.out;
	private static final  int NUMBER_OF_ACCOUNTS  = 20;
	
	/**
	 * main method
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		out.println( "Reading data from " + infilename + " ...");
		
		// Open the named file for reading
		java.io.FileInputStream fIn = openInputFile( infilename ); 
		if (fIn == null)
			throw new Exception("Bad file name!!");
		Scanner fileReader = new Scanner( fIn );
		fileReader.nextLine();

		/**
		 * Set up arrays with enough space to read in data.
		 */
		Account[] accounts = new Account[NUMBER_OF_ACCOUNTS];
		/*
		 * Read all lines of the file
		 *  - assuming the NUMBER_OF_ACCOUNTS is set correctly
		 */
		for (int i=0; i<NUMBER_OF_ACCOUNTS; i++){
			String last  = fileReader.next();
			String first = fileReader.next();
			int    id    = fileReader.nextInt();
			float  bal   = fileReader.nextFloat();
			accounts[i] = new Account(id, bal, first, last);
		}
		/*
		 * Close the input file and serialize the "low" accounts to a file
		 */
		closeFile(fIn);
		ObjectOutputStream os = new ObjectOutputStream( new FileOutputStream(folder+"LowAccounts") );
		for (Account a: accounts)
			if (a.getBalance() < Account.MINIMUM_BALANCE)
				os.writeObject( a );
		os.close();

		/*
		 * Read the LowAccounts file back in and print out the contents. 
		 */
		Account[] lows = new Account[NUMBER_OF_ACCOUNTS];
		ObjectInputStream is = new ObjectInputStream( new FileInputStream(folder+"LowAccounts") );
		Object in = null; int index = 0;
		do {
			try {
				in = is.readObject();
				lows[ index++ ] = (Account) in;
			} catch (java.io.EOFException e){
				in = null;
			}
		} while (in != null );
		out.println(" " + index + " objects read from low file:");
		for (Account a: lows)
			if (a != null)
				out.println( a );				

	}
	
	/**
	 * openFile - a method that opens a file for reading.
	 * 
	 * @param fname - name of the input file
	 */
	private static java.io.FileInputStream openInputFile(String fname) {
		java.io.FileInputStream fIn = null;
		try {
			fIn = new java.io.FileInputStream( fname );
		} catch(java.io.FileNotFoundException fex){
			System.err.println(" Error! This program is trying to open " + fname + " but it does not exist! Or it's some place else.");
		}
		return fIn;
	}
	
	/**
	 * closeFile - a method that closes a file 
	 *    that was previously opened for reading
	 *    or writing.
	 * 
	 * @param fs - an open FileInputStream or FileOutputStream object
	 */
	private static void closeFile(java.io.Closeable fs) {
		if (fs == null)
			return; // nothing to close
		try {
			fs.close();
		} catch (IOException e) {
			System.err.println(" Error! This program is trying to close a file but something went wrong.");
		}
	}
}
